var baseurl = "http://localhost:5500";
layui.use('element', function () {
    var element = layui.element;
})
function loadData() {
    loadAllArea();
    loadHotPostList();
    loadBoard();
}
function loadAllArea() {
    $.ajax({
        url: 'http://localhost:8080/area/list-all-with-blocks',
        method: 'POST',
        crossDomain: true,
        xhrFields: { withCredentials: true },
        success: function (msg) {
            //拼装版块
            let data = msg.data.list;

            $(data).each(function (i, element) {

                let area = "<div class='area-div'>\
                <div class='area-name-div'>" + " <span>"
                    + element.area.name + "</span>";
                area += "<span class='area-desc'>帖子数 / 回复数</span>";
                area += "</div>";
                $(element.blocks).each(function (j, block) {
                    area += "<div class='block-div'>";
                    area += "<div class='block-title'>";
                    // area += "<span>" + block.name + "</span>";
                    area += "<a href='" + "./block/detail.html?id=" + block.id + "'>";
                    area += block.name + "</a>";
                    area += "</div>";
                    area += "<div class='block-info'>";
                    area += "<span>" + block.post_count + "/" + block.reply_count + "</span>";
                    area += "</div>";
                    area += "</div>";
                });
                area += "</div>";
                console.log(area);
                $("#area-list-div").append(area);
            });
        }, error: function (error) {
            layer.msg("版块获取失败~")
        }
    });
}
function loadHotPostList() {
    $.ajax({
        url: 'http://localhost:8080/post/list-all-hot-posts',
        method: 'POST',
        data: {
            "num": 9
        },
        crossDomain: true,
        xhrFields: { withCredentials: true },
        success: function (msg) {
            let list = msg.data.list;
            console.log(list)
            $(list).each(function (i, e) {
                let str = "<li>";
                str += "<span style='width: 70%'><a href='./post/detail.html?id=" + e.id + "'>";
                str += "`-" + e.title + "</a></span>";
                str += "<span style='text-align: center; width: 30px'>" + e.replyCountInTime + "</span>";
                str += "<span class='hot-post-author'><a href='./user/info.html?id=" + e.authorId + "'>" + e.authorName + "</a></span>";
                str += "</li>";
                $(".hot-post-list-inner").append(str);
            })

        }, error: function (error) {
            layer.msg("热帖获取失败~")
        }
    })
}
/**
 * 只查询前8条，多了装不下，根据发布时间最近
 * 
 * 
 * */
function loadBoard() {
    $.ajax({
        url: 'http://localhost:8080/board-message/list',
        method: 'POST',
        data: {
            "num": 9
        },
        crossDomain: true,
        xhrFields: { withCredentials: true },
        success: function (msg) {
            let list = msg.data.list;
            console.log(list)
            $(list).each(function (i, e) {
                let str = "<li><a href='./board/detail.html?id=" + e.id + "'>";
                str += "`-" + e.title + "</a>";
                str += "<div class='msg-create-time'>" + e.createTime + "</div>";
                str += "</li>"
                $(".board-message-list-inner").append(str);
            })

        }, error: function (error) {
            layer.msg("公告获取失败~")
        }
    })
}